package com.bs.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.bs.bean.Comment;
import com.bs.bean.User;
import com.bs.service.CommentService;
import com.bs.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private VideoService videoService;

//    @Value("${comment.avatar}")
//    private String avatar;

    @GetMapping("/ct")
    public String comment() {
        return "comment";
    }

    @GetMapping("/comment")
    public String comments( Model model, HttpSession session) {
        Integer videoId = (Integer)session.getAttribute("videoId");
        List<Comment> comments = commentService.listByIdComment(videoId);
        model.addAttribute("videoId", videoId);
        model.addAttribute("comments", comments);
        return "comment :: commentList";
    }

    @PostMapping("/comment")
    public String post(Comment comment, HttpSession session) {
        //设置头像
        User user = (User)session.getAttribute("user");
        String avatar = user.getPicture();
        comment.setAvatar(avatar);
        Integer videoId = (Integer)session.getAttribute("videoId");
        comment.setVideoId(videoId);
        if (comment.getParentComment().getId() != null) {
            comment.setParentCommentId(comment.getParentComment().getId());
        }
        commentService.saveComment(comment);
        videoService.addVideoTc(videoId);
        return "redirect:/comment";
    }

    @RequestMapping(value = "/getAllCommentByName")
    public String getAllCommentByName(String nickname, @RequestParam(required = false, value = "pageNum", defaultValue = "1") Integer pageNum,
                              ModelMap model) {
        PageHelper.startPage(pageNum, 5);
        ArrayList<Comment> list = commentService.getAllCommentByName(nickname);

        PageInfo pageInfo = new PageInfo(list,5);
        model.addAttribute("commentList", pageInfo);

        return "frontPage/commentList";
    }

    @GetMapping(value = "/getAllCommentByAdmin")
    public String getAllAnswerByAdmin(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                      ModelMap model) {
        PageHelper.startPage(pageNum,5);
        PageHelper.orderBy("create_time desc");

        List<Comment> commentList = commentService.getAllComment();

        PageInfo pageInfo = new PageInfo(commentList);
        model.addAttribute("commentList",pageInfo);

        return "admin/comment/commentList";
    }

    @GetMapping(value = "/getCommentByName")
    public String getCommentByName(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                 String nickname,ModelMap model) {
        PageHelper.startPage(pageNum,5);
        PageHelper.orderBy("create_time desc");
        List<Comment> commentList = commentService.getCommentByName(nickname);

        PageInfo pageInfo = new PageInfo(commentList);
        model.addAttribute("commentList",pageInfo);
        return "admin/comment/commentList";
    }



    @GetMapping(value = "/deleteCommentByIdAdmin")
    public String deleteAnswerByIdAdmin(Integer id) {

        Comment comment = commentService.getCommentById(id);
        Integer videoId = comment.getVideoId();
        if(comment.getState2()==1){
            comment.setState2(2);
        }else{
            comment.setState2(1);
        }
        commentService.updateStateByComment(comment);
        boolean flag = commentService.deleteCommentById(id);


        if(flag) {
            videoService.delVideoTC(videoId);
            return "forward:/getAllCommentByAdmin";
        }

        return "failed";
    }

    @GetMapping(value = "/updateCommentStateByIdAdmin")
    public String updateAnswerStateByIdAdmin(Integer id) {
        Comment comment = commentService.getCommentById(id);
        if(comment.getState2()==1){
            comment.setState2(2);
        }else{
            comment.setState2(1);
        }
        commentService.updateStateByComment(comment);

        return "forward:/getAllAnswerByAdmin";
    }

//    @GetMapping(value = "/deleteCommentById")
//    public String deleteTopicById(Integer id, HttpServletRequest request) {
//        boolean result = commentService.deleteCommentById(id);
//        if (result) {
//            User user = (User) request.getSession().getAttribute("user");
//            String nickname = user.getUsername();
//            return "redirect:getAllCommentByName?nickname=" + nickname;
//        }
//        return "frontPage/commentFailed";
//    }
}